#coding=utf-8
import datetime
import time


class Time(object):
    def __init__(self,file_path):
        self.file_path = file_path

    # 这个方法的功能是用来控制脚本运行的时间
    def time_test(self):
        time_delta = datetime.timedelta(minutes=2)  # 时间差变量
        mark_time = datetime.datetime(year=2016, month=12, day=18, hour=14, minute=58, second=0)  # 定义标识时间
        try:
            while True:
                file = open(self.file_path)
                current_time = datetime.datetime.now()
                if current_time > mark_time:
                    print("run job")
                    mark_time = mark_time + time_delta
                    print("mark_time: {0}".format(mark_time))
                    file.write("run job \n {0} \n".format(mark_time))
                    time.sleep(3)
                    continue
                else:
                    print("current_time < mark_time,current_time is {0},so job is stop".format(current_time))
                    file.write("current_time < mark_time,current_time is {0},so job is stop \n".format(current_time))
                    time.sleep(3)
        except FileNotFoundError as fileNotFind:
            print("{0},Double check wheather file is exist".format(fileNotFind))